<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.time.format.DateTimeFormatter" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生考试管理系统 - 仪表盘</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.8/dist/chart.umd.min.js"></script>
    <style>
        .card {
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        .card-header {
            background-color: #f8f9fa;
            border-bottom: none;
            font-weight: bold;
        }
        .stats-card {
            text-align: center;
            padding: 20px;
        }
        .stats-icon {
            font-size: 3rem;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
<!-- 导航栏 -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <div class="container">
        <a class="navbar-brand" href="dashboard">
            <i class="fa fa-graduation-cap mr-2"></i>学生考试管理系统
        </a>

        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav">
                <li class="nav-item active">
                    <a class="nav-link" href="dashboard">仪表盘</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="exams">在线考试</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="scores">成绩查询</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="profile">个人信息</a>
                </li>
            </ul>

            <ul class="navbar-nav ms-auto">
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fa fa-user-circle mr-1"></i>${sessionScope.student.name} (${sessionScope.student.studentId})
                    </a>
                    <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                        <li><a class="dropdown-item" href="profile">个人信息</a></li>
                        <li><a class="dropdown-item" href="changePassword">修改密码</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item" href="logout">退出登录</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</nav>

<!-- 主内容 -->
<div class="container mt-4">
    <div class="row">
        <!-- 统计卡片 -->
        <div class="col-md-3">
            <div class="card stats-card bg-primary text-white">
                <div class="stats-icon">
                    <i class="fa fa-calendar-check-o"></i>
                </div>
                <h3>即将进行的考试</h3>
                <p class="fs-1">${upcomingExamsCount}</p>
            </div>
        </div>

        <div class="col-md-3">
            <div class="card stats-card bg-success text-white">
                <div class="stats-icon">
                    <i class="fa fa-check-circle"></i>
                </div>
                <h3>已完成的考试</h3>
                <p class="fs-1">${completedExamsCount}</p>
            </div>
        </div>

        <div class="col-md-3">
            <div class="card stats-card bg-info text-white">
                <div class="stats-icon">
                    <i class="fa fa-line-chart"></i>
                </div>
                <h3>平均成绩</h3>
                <p class="fs-1"><fmt:formatNumber value="${averageScore}" maxFractionDigits="1" minFractionDigits="1"/></p>
            </div>
        </div>
        
        <div class="col-md-3">
            <div class="card stats-card bg-danger text-white">
                <div class="stats-icon">
                    <i class="fa fa-times-circle"></i>
                </div>
                <h3>挂科科目</h3>
                <p class="fs-1">${failedExamsCount}</p>
            </div>
        </div>
    </div>

    <div class="row mt-4">
        <!-- 即将进行的考试 -->
        <div class="col-md-4">
            <div class="card">
                <div class="card-header">
                    <i class="fa fa-bell mr-2"></i>即将开始的考试
                </div>
                <div class="card-body">
                    <c:choose>
                        <c:when test="${not empty upcomingExams}">
                            <div class="list-group">
                                <c:forEach var="exam" items="${upcomingExams}">
                                    <a href="${pageContext.request.contextPath}/exams/take/${exam.examId}" class="list-group-item list-group-item-action">
                                        <div class="d-flex w-100 justify-content-between">
                                            <h6 class="mb-1">${exam.examName}</h6>
                                            <small class="text-muted">${exam.startTime.format(DateTimeFormatter.ofPattern("MM-dd HH:mm"))}</small>
                                        </div>
                                        <small class="text-muted">${exam.duration} 分钟</small>
                                    </a>
                                </c:forEach>
                            </div>
                        </c:when>
                        <c:otherwise>
                            <p class="text-center text-muted">暂无即将开始的考试</p>
                        </c:otherwise>
                    </c:choose>
                </div>
            </div>
        </div>

        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <i class="fa fa-bar-chart mr-2"></i>成绩统计
                </div>
                <div class="card-body">
                    <canvas id="scoreChart"></canvas>
                </div>
            </div>
        </div>
    </div>

    <div class="row mt-4">
        <!-- 最近成绩 -->
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <i class="fa fa-history mr-2"></i>最近完成的考试
                </div>
                <div class="card-body">
                    <c:choose>
                        <c:when test="${not empty recentScores}">
                            <ul class="list-group list-group-flush">
                                <c:forEach var="score" items="${recentScores}">
                                    <li class="list-group-item d-flex justify-content-between align-items-center">
                                        ${score.exam.examName}
                                        <span class="badge ${score.score >= 60 ? 'bg-primary' : 'bg-danger'} rounded-pill">${score.score} 分</span>
                                    </li>
                                </c:forEach>
                            </ul>
                        </c:when>
                        <c:otherwise>
                            <p class="text-center text-muted">暂无成绩记录</p>
                        </c:otherwise>
                    </c:choose>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 页脚 -->
<footer class="bg-light text-center text-muted mt-5 py-3">
    <div class="container">
        <p>© 2025 学生考试管理系统 | 由管理员分配账户</p>
    </div>
</footer>

<script>
    // 初始化成绩统计图表
    document.addEventListener('DOMContentLoaded', function() {
        const ctx = document.getElementById('scoreChart').getContext('2d');
        
        // 从JSP获取数据
        const examNames = [];
        const scores = [];
        
        <c:forEach var="score" items="${recentScores}">
            examNames.push('${score.exam.examName}');
            scores.push(${score.score});
        </c:forEach>

        // 创建图表
        if (examNames.length > 0) {
            new Chart(ctx, {
                type: 'bar',
                data: {
                    labels: examNames,
                    datasets: [{
                        label: '成绩',
                        data: scores,
                        backgroundColor: 'rgba(13, 110, 253, 0.7)',
                        borderColor: 'rgb(13, 110, 253)',
                        borderWidth: 1
                    }]
                },
                options: {
                    responsive: true,
                    scales: {
                        y: {
                            beginAtZero: true,
                            max: 100
                        }
                    }
                }
            });
        } else {
             // 如果没有数据，可以显示一个消息
            ctx.font = "16px Arial";
            ctx.fillStyle = "#888";
            ctx.textAlign = "center";
            ctx.fillText("暂无成绩数据可用于生成图表", ctx.canvas.width / 2, 50);
        }
    });
</script>
</body>
</html> 